package top.ochiamalu.aioj.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.ochiamalu.aioj.common.BaseResponse;
import top.ochiamalu.aioj.service.FileService;
import top.ochiamalu.aioj.util.ResultUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 文件上传控制器
 *
 * @author ochiamalu
 * @date 2025/02/10
 */
@RestController
@RequestMapping("/files")
public class FileController {

    @Resource
    private FileService fileService;

    /**
     * 上传图片
     *
     * @param file    文件
     * @return {@link BaseResponse}<{@link Boolean}>
     */
    @PostMapping("/upload")
    public BaseResponse<String> uploadImage(@RequestBody MultipartFile file) {
        String url = fileService.uploadImage(file);
        return ResultUtils.success(url);
    }

    /**
     * 下载
     *
     * @param name     名字
     * @param response 响应
     */
    @GetMapping("/{name}")
    public void download(@PathVariable String name, HttpServletResponse response) {
        fileService.downloadFile(name, response);
    }
}
